package com.sssre.business.util;

import com.sssre.common.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 浪兎兎
 * @create 2025-04-30-11:11
 */
public abstract class DateUtil {
    /**
     * 30分钟
     */
    public static final Long HALF_HOUR = 1800000L;

    /**
     * 时间加1天减1秒
     */
    public static Date getEndTime4Day(Date date) {
        if (date == null) return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.add(Calendar.SECOND, -1);
        return calendar.getTime();
    }
    /**
     * 日期转字符串
     */
    public static String toDateString(String pattern,Date date) {
        if (StringUtils.isEmpty(pattern) || date == null) return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        String newDate = null;
        try {
            newDate = simpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newDate;
    }

    /**
     * 字符串转日期
     */
    public static Date toDateString(String pattern,String date) {
        if (StringUtils.isEmpty(pattern) || StringUtils.isEmpty(date)) return null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date newDate = null;
        try {
            newDate = simpleDateFormat.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newDate;
    }

    /**
     * 时间必须为半点或整点
     */
    public static boolean isHalfPointOrWholePoint(Date appointmentTime) {
        if (appointmentTime == null) return false;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(appointmentTime);
        int minute = calendar.get(Calendar.MINUTE);
        return !(minute != 30 && minute != 0);
    }

    /**
     * 拼接时间 年月日 和 时分秒
     */
    public static Date montage(Date date, Date time) {
        if (date == null || time == null) return null;
        // 年月日
        Calendar calendar4Date = Calendar.getInstance();
        calendar4Date.setTime(date);
        // 时分
        Calendar calendar4Time = Calendar.getInstance();
        calendar4Time.setTime(time);
        // 添加
        calendar4Date.add(Calendar.HOUR, calendar4Time.get(Calendar.HOUR));
        calendar4Date.add(Calendar.MINUTE, calendar4Time.get(Calendar.MINUTE));
        return calendar4Date.getTime();
    }

    /**
     * 两时间相减
     */
    public static Date minus(Date endTime, Date startTime) {
        // 校验 endTime 大于 startTime
        if (endTime.getTime() <= startTime.getTime()) {
            throw new IllegalStateException("结束时间不能大于开始时间");
        }
        // 结束时间
        Calendar calendar4EndTime = Calendar.getInstance();
        calendar4EndTime.setTime(endTime);
        // 开始时间
        Calendar calendar4StartTime = Calendar.getInstance();
        calendar4StartTime.setTime(startTime);
        // 相减
        calendar4EndTime.add(Calendar.YEAR, -calendar4StartTime.get(Calendar.YEAR));
        calendar4EndTime.add(Calendar.MONTH, -calendar4StartTime.get(Calendar.MONTH));
        calendar4EndTime.add(Calendar.DAY_OF_MONTH, -calendar4StartTime.get(Calendar.DAY_OF_MONTH));
        calendar4EndTime.add(Calendar.HOUR_OF_DAY, -calendar4StartTime.get(Calendar.HOUR_OF_DAY));
        calendar4EndTime.add(Calendar.MINUTE, -calendar4StartTime.get(Calendar.MINUTE));
        calendar4EndTime.add(Calendar.SECOND, -calendar4StartTime.get(Calendar.SECOND));
        return calendar4EndTime.getTime();
    }
    /**
     * 获取分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }
}
